package com.iisigroup.audf.statistics;

import java.io.FileNotFoundException;
import java.io.IOException;

/**
 *
 * @author whchen
 */
public class Deviation {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
//        try{
//            System.out.println("檔案內容的標準差 = "+ calculate("D:\\b.txt"));
//        }catch(Exception ex){
//            ex.printStackTrace();
//        }
	long[] data = new long[]{123L ,456L ,789L,159L,357L,111L,222L, 333L };
	System.out.println(calculate(data));
	System.out.println(Math.sqrt(4));
	System.out.println(Math.pow(3, 3.0));
	;
    }
    
    public static double calculate(String Path) throws FileNotFoundException, IOException {
        java.io.BufferedReader br = new java.io.BufferedReader(new java.io.FileReader(Path)) ;
        /*http://zh.wikipedia.org/wiki/%E6%A8%99%E6%BA%96%E5%B7%AE  標準差公式*/
        
        //初始化這個List
        java.util.List<Double> mList = new java.util.ArrayList<Double>(10);
        
        
        String tmp  = "";
        //讀檔讀到檔尾
        while ( (tmp = br.readLine()) != null) {
            mList.add(Double.parseDouble(tmp));
        } 
        
        br.close() ;
        
        //算出平均值
        double a = 0.0 ;
        double b = 0.0 ;
        for(double t : mList){
            a += t;
            b += Math.pow(t, 2.0) ;
        }
        
        a = a / mList.size() ;
        b = b / mList.size() ;
        
        //計算出標準差
        return Math.sqrt( b - Math.pow(a, 2.0)) ;
    }
    public static double calculate2(long[] data){
	 //算出平均值
	long a =0L;
	long b =0L;
	for(long t: data){
	    a += t;
           b += Math.pow(t, 2.0) ;
	}
	a = a/((long)data.length);
	b = b/((long)data.length);
	return Math.sqrt( b - Math.pow(a, 2.0)) ;
   }
    public static double calculate(long[] data){
	 //算出平均值
	long a =0L;
	long b =0L;
	for(long t: data){
	    a += t;
            b += Math.pow(t, 2.0) ;
	}
	a = a/((long)data.length);
	b = b/((long)data.length);
	return Math.sqrt( b - Math.pow(a, 2.0)) ;
    }
}

